#导入包
import sys
sys.path.append('/usr/local/lib/python3.9/site-packages')
import ollama
#初始化ollama
client = ollama.Client("http://localhost:11434")
#client = ollama.Client()
#定义模型名称
model_name = "llama3.1:8b"
#输入提示
prompt = "Once upon a time in a land far ,far away,there was a kingdom ruled by a wise and just king."
#调用模型生成文本
response = client.generate(model_name,prompt)
#检查返回对象的类型和结构
if isinstance(response,dict):
        gen_text = response.get('gen_text','')
elif hasattr(response,'text'):
        gen_text = response.get(text)
elif hasattr(response,'gen_text'):
        gen_text = response.gen_text
else:
        gen_text = str(response)
#输出生成结果
print(gen_text)